﻿using AnesSystem.Models;
using AnesSystem.Services;
using AnesSystem.Util;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;

namespace AnesSystem.ViewModels.WorkScore.CQRCQRMYY
{
    public class WorkSocreStatisticsRCViewModel : BaseViewModel<WorkSocreStatisticsRCViewModel>
    {
        #region Variables
        private DateTime _startTime;
        private DateTime _endTime;
        private string _docName;
        private ObservableCollection<WorkSocreRCModel> _showDataList;

        #endregion


        #region Constructor
        public WorkSocreStatisticsRCViewModel()
        {
            InitData();
        }


        #endregion


        #region Properties
        public DateTime StartTime
        {
            get
            {
                return _startTime;
            }

            set
            {
                _startTime = value;
                RaisePropertyChanged("StartTime");
            }
        }

        public DateTime EndTime
        {
            get
            {
                return _endTime;
            }

            set
            {
                _endTime = value;
                RaisePropertyChanged("EndTime");
            }
        }

        public string DocName
        {
            get
            {
                return _docName;
            }

            set
            {
                _docName = value;
                RaisePropertyChanged("DocName");
            }
        }

        public ObservableCollection<WorkSocreRCModel> ShowDataList
        {
            get
            {
                return _showDataList;
            }

            set
            {
                _showDataList = value;
            }
        }



        #endregion


        #region 依赖属性


        #endregion


        #region Event


        #endregion


        #region PrivateMethods

        private void InitData()
        {
            ShowDataList = new ObservableCollection<WorkSocreRCModel>();
            DateTime start = DateTime.Now;
            start = start.AddMonths(-1).AddHours(-start.Hour).AddMinutes(-start.Minute).AddSeconds(-start.Second);
            StartTime = start;

            DateTime end = DateTime.Now;
            end = end.AddHours(-end.Hour).AddHours(23).AddMinutes(-end.Minute).AddMinutes(59).AddSeconds(-end.Second).AddSeconds(59);
            EndTime = end;
        }

        private bool CheckInThisOper(PatientLinkModel item)
        {
            bool flag = false;
            string name = DataManager.Instance.GetAccountNameByID(item.AnesthesiaRecord.Zmys) + item.AnesthesiaRecord.Zmys;
            if (name.Contains(DocName))
            {
                flag = true;
            }
            return flag;
        }

        private void CalData(List<PatientLinkModel> patientLinkList)
        {
            Dictionary<string, List<PatientLinkModel>> dataMap = new Dictionary<string, List<PatientLinkModel>>();
            foreach (var item in patientLinkList)
            {
                string month = item.Status.OutRoomTime.Month.ToString();
                if (dataMap.ContainsKey(month))
                {
                    dataMap[month].Add(item);
                }
                else
                {
                    List<PatientLinkModel> tempList = new List<PatientLinkModel>();
                    tempList.Add(item);
                    dataMap.Add(month, tempList);
                }
            }
            foreach (var item in dataMap.Keys)
            {
                WorkSocreRCModel workSocre = new WorkSocreRCModel();
                workSocre.ShowDate = item+"月";
                List<PatientLinkModel> tempList = dataMap[item];
                foreach (var iItem in tempList)
                {
                    workSocre.QuanShengMazuiShuangQiang = (double.Parse(workSocre.QuanShengMazuiShuangQiang)+ iItem.Gongfen.QuanMaShuangQiang).ToString();
                    workSocre.QuanShengMazuiDanQiang = (double.Parse(workSocre.QuanShengMazuiDanQiang) + iItem.Gongfen.QuanMaDanQiang).ToString();
                    workSocre.FeiChaGuanQuanMa = (double.Parse(workSocre.FeiChaGuanQuanMa) + iItem.Gongfen.FeiChaGuanQuanMa).ToString();
                    workSocre.YaoYingLianHe = (double.Parse(workSocre.YaoYingLianHe) + iItem.Gongfen.YaoYingLianHe).ToString();
                    workSocre.YingMoWai = (double.Parse(workSocre.YingMoWai) + iItem.Gongfen.YingMoWai).ToString();
                    workSocre.ShenJingZuZhi = (double.Parse(workSocre.ShenJingZuZhi) + iItem.Gongfen.ShenJingZuZhi).ToString();
                    workSocre.FuHeMazui = (double.Parse(workSocre.FuHeMazui) + iItem.Gongfen.FuHeMazui).ToString();
                    workSocre.DongMaiChuanCi = (double.Parse(workSocre.DongMaiChuanCi) + iItem.Gongfen.DongMaiChuanCi).ToString();
                    workSocre.JingMaiChuanCi = (double.Parse(workSocre.JingMaiChuanCi) + iItem.Gongfen.ZhongXinJingMaiChuanCi).ToString();
                    workSocre.ShuZhongChaoSheng = (double.Parse(workSocre.ShuZhongChaoSheng) + iItem.Gongfen.ShuZhongChaoSheng).ToString();
                    workSocre.ShuZhongXueQiFenXi = (double.Parse(workSocre.ShuZhongXueQiFenXi) + iItem.Gongfen.ShuZhongXueQiFenXi).ToString();
                    workSocre.QingJiuQiGuanChaGuan = (double.Parse(workSocre.QingJiuQiGuanChaGuan) + iItem.Gongfen.QiangJiuQiGuanChaGuan).ToString();
                    workSocre.ShuHouZhenTong = (double.Parse(workSocre.ShuHouZhenTong) + iItem.Gongfen.ShuHouZhenTong).ToString();
                    workSocre.QuanMaJianCe = (double.Parse(workSocre.QuanMaJianCe) + iItem.Gongfen.QuanMaJianCe).ToString();
                    workSocre.FeiQuanMaJianCe = (double.Parse(workSocre.FeiQuanMaJianCe) + iItem.Gongfen.FeiQuanMaJianCe).ToString();
                    workSocre.Total = (double.Parse(workSocre.Total) + iItem.Gongfen.Total).ToString();
                }
                ShowDataList.Add(workSocre);
            }
            WorkSocreRCModel workSocreTotal = new WorkSocreRCModel();
            workSocreTotal.ShowDate = "总分";
            foreach (var item in ShowDataList)
            {
                workSocreTotal.QuanShengMazuiShuangQiang = (double.Parse(workSocreTotal.QuanShengMazuiShuangQiang) + double.Parse(item.QuanShengMazuiShuangQiang)).ToString();
                workSocreTotal.QuanShengMazuiDanQiang = (double.Parse(workSocreTotal.QuanShengMazuiDanQiang) + double.Parse(item.QuanShengMazuiDanQiang)).ToString();
                workSocreTotal.FeiChaGuanQuanMa = (double.Parse(workSocreTotal.FeiChaGuanQuanMa) + double.Parse(item.FeiChaGuanQuanMa)).ToString();
                workSocreTotal.YaoYingLianHe = (double.Parse(workSocreTotal.YaoYingLianHe) + double.Parse(item.YaoYingLianHe)).ToString();
                workSocreTotal.YingMoWai = (double.Parse(workSocreTotal.YingMoWai) + double.Parse(item.YingMoWai)).ToString();
                workSocreTotal.ShenJingZuZhi = (double.Parse(workSocreTotal.ShenJingZuZhi) + double.Parse(item.ShenJingZuZhi)).ToString();
                workSocreTotal.FuHeMazui = (double.Parse(workSocreTotal.FuHeMazui) + double.Parse(item.FuHeMazui)).ToString();
                workSocreTotal.DongMaiChuanCi = (double.Parse(workSocreTotal.DongMaiChuanCi) + double.Parse(item.DongMaiChuanCi)).ToString();
                workSocreTotal.JingMaiChuanCi = (double.Parse(workSocreTotal.JingMaiChuanCi) + double.Parse(item.JingMaiChuanCi)).ToString();
                workSocreTotal.ShuZhongChaoSheng = (double.Parse(workSocreTotal.ShuZhongChaoSheng) + double.Parse(item.ShuZhongChaoSheng)).ToString();
                workSocreTotal.ShuZhongXueQiFenXi = (double.Parse(workSocreTotal.ShuZhongXueQiFenXi) + double.Parse(item.ShuZhongXueQiFenXi)).ToString();
                workSocreTotal.QingJiuQiGuanChaGuan = (double.Parse(workSocreTotal.QingJiuQiGuanChaGuan) + double.Parse(item.QingJiuQiGuanChaGuan)).ToString();
                workSocreTotal.ShuHouZhenTong = (double.Parse(workSocreTotal.ShuHouZhenTong) + double.Parse(item.ShuHouZhenTong)).ToString();
                workSocreTotal.QuanMaJianCe = (double.Parse(workSocreTotal.QuanMaJianCe) + double.Parse(item.QuanMaJianCe)).ToString();
                workSocreTotal.FeiQuanMaJianCe = (double.Parse(workSocreTotal.FeiQuanMaJianCe) + double.Parse(item.FeiQuanMaJianCe)).ToString();
                workSocreTotal.Total = (double.Parse(workSocreTotal.Total) + double.Parse(item.Total)).ToString();
            }
            ShowDataList.Add(workSocreTotal);
        }
        #endregion


        #region Public/ProtectedMethods

        public void ExportExcel(DataGrid datagrid)
        {
            ExportToExcel.Instance.Excel.ExportParameterCommon("工分导出", datagrid);
        }

        public void SearchData()
        {
            if (string.IsNullOrEmpty(DocName))
            {
                MessageBoxCommon.ShowDialog("请输入医生姓名!", MessageBoxCommon.OK);
                return;
            }
            ShowDataList.Clear();
            if (EndTime < StartTime)
            {
                MessageBoxCommon.ShowDialog("查询开始时间应小于结束时间!", MessageBoxCommon.OK);
                return;
            }
            if (StartTime.Year != EndTime.Year)
            {
                StartTime.AddYears(-StartTime.Year).AddYears(EndTime.Year).AddMonths(-StartTime.Month).AddMonths(1).AddDays(-StartTime.Day).AddDays(1);
            }
            StartTime = StartTime.AddHours(-StartTime.Hour).AddMinutes(-StartTime.Minute).AddSeconds(-StartTime.Second);
            EndTime = EndTime.AddHours(-EndTime.Hour).AddHours(23).AddMinutes(-EndTime.Minute).AddMinutes(59).AddSeconds(-EndTime.Second).AddSeconds(59);
            List<PatientLinkModel> patientLinks = PatientLinkService.Instance.GetFinishOperByTime(StartTime, EndTime);
            List<PatientLinkModel> patientLinkList = new List<PatientLinkModel>();
            foreach (var item in patientLinks)
            {
                if (CheckInThisOper(item))
                {
                    patientLinkList.Add(item);
                }
            }
            CalData(patientLinkList);
        }


        #endregion


        #region EventHandlers


        #endregion
    }
}
